<template>
    <div class="home">
        <header-nav></header-nav>
        <div class="container">
            <div class="right">
                <side-bar></side-bar>
                <side-bar-top></side-bar-top>
            </div>
            <div class="content">
                <div class="panel">
                    <div class="header">
                        <span @click="clickTab('')" class="topic-tab"  :class="{ 'current-tab': current === ''}">全部</span>
                        <span v-for="item in tab_type" 
                              :key="item.key" 
                              @click="clickTab(item.key)" 
                              :class="{ 'current-tab': current === item.key}"
                              class="topic-tab" 
                              >{{item.value}}</span>
                    </div>
                    <div class="inner no-padding">
                        <div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" :infinite-scroll-distance="30">
                            <div class="cell" v-for="(item, index) in topics" :key="index">
                                <router-link :to="'/user/' + item.author_name" class="user_avatar left">
                                    <img :src="require('@/assets/images/avatar.jpg')" alt="">
                                </router-link>
                                <div class="topic_title_wrapper">
                                    <div class="clear">
                                        <span class="type_tag type_share left">{{item.tab_value}}</span>
                                        <router-link :to="'/topic/' + item._id" class="topic_title ellipsis left">{{item.title}}</router-link>
                                    </div>
                                    <div class="clear infos">
                                        <div class="icons-list left">
                                            <div class="icons left" title="回复数">
                                                <i class="fa fa-comment-o"></i> 
                                                <span class="number">{{item.reply_count}}</span>
                                            </div>
                                            <div class="icons left" title="点击数">
                                                <i class="fa fa-eye"></i> 
                                                <span class="number">{{item.visit_count}}</span>
                                            </div>
                                            <div class="icons left" title="收藏数">
                                                <i class="fa fa-star"></i> 
                                                <span class="number">{{item.collect_count}}</span>
                                            </div>
                                            <router-link :to="'/user/' + item.author_name" class="icons left">
                                                <span class="fontS13">来自作者·{{item.author_name}}</span>
                                            </router-link>
                                        </div>
                                        <span class="last_active_time right" v-if="item.last_reply_at !== null">最新动态 {{days(item.last_reply_at)}}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import HeaderNav from "@/components/header/header"
import SideBarTop from "@/components/sidebar/sidebartop"
import SideBar from "@/components/sidebar/sidebar"
import { getTopic, topicType } from "@/service/api"
import { relativeTime } from "@/config/mUtils"

export default {
    data() {
        return {
            topics: [],
            tab_type: [],
            current: '', //当前选择的板块
            isLoading: true, //是否加载数据
            page: 1,
            busy: false,
            noResult: false, //数据已经加载完了
        }
    },
    mounted() {
        this.getTopics('')
        this.getType()
    },
    methods: {
        days(time) {
            return relativeTime(time)
        },
        clickTab(query) {
            this.current = query
            this.topics = []
            this.page = 0
            this.getTopics()
        },
        async getTopics() {
            let query = this.current
            let param = {
                tab_key: query,
                page: this.page
            }
            if(query === ''){
                param = {
                    page: this.page
                }
            }
            let res = await getTopic(param)
            if(res.code) {
                if(res.result.length ===0) {
                    this.noResult = true;
                }
                // this.topics.concat(res.result) //为什么不好使？？？？
                this.topics.push.apply(this.topics, res.result)
            } else {
                this.$message.error(res.msg)
            }
        },
        async getType() {
            let res = await topicType()
            if(res.code) {
                this.tab_type = res.result
            } else {
                this.$message.error(res.msg)
            }
        },
        loadMore() {
            let current = this.current
            this.busy = true
            //加载完数据后就不再请求
            if(this.noResult) {return}
            this.page++
            this.getTopics(current)
            this.busy = false
        }
    },
    components: {
        HeaderNav,
        SideBarTop,
        SideBar,
    }
}
</script>


<style lang="scss" scoped>
    @import '../../style/mixin.scss';

    .bottom {
        border: 1px solid red;
    }
    .home {
        .container {
            margin-top: 10px;
            .topic-tab {
                font-size: 14px;
                color: $main;
                margin: 0 5px;
                padding: 3px 4px;
                border-radius: 3px;
                cursor: pointer;
            }
            .current-tab {
                background-color: $main;
                color: $fff;
            }
            .cell {
                &:hover {
                    background-color: $title-bg;
                }
                position: relative;
                border-bottom: 1px solid #ddd;
                padding: 10px;
                width: 100%;
                .icons-list {
                    padding-top: 10px;
                }
                .user_avatar {
                    @include ct;
                    width: 50px;
                    height: 50px;
                    border-radius: 3px;
                    overflow: hidden;
                }
                .last_active_time {
                    text-align: right;
                    min-width: 50px;
                    display: inline-block;
                    white-space: nowrap;
                    color: $gray;
                    font-size: 12px;
                    padding-top: 15px;
                }
                .topic_title_wrapper {
                    padding-left: 70px;
                }
                
            }
        }
    }
    
</style>

